home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d886.lha / TextPort / source / StripHR.c < prev   
C/C++ Source or Header  |  1993-07-16  |  4KB  |  118 lines

  1. /*------------------------------------------------------------+
  2.  | striphr.c -- 5/15/1992 public domain                       |
  3.  |             by Alex Matulich, Unicorn Research Corporation |
  4.  | Strip hard carriage returns from a text file in such a way |
  5.  | that the paragraph formatting is not lost.  A newline char |
  6.  | will be kept in the file if:                               |
  7.  |       (1)   It is followed by whitespace, or               |
  8.  |       (2)   It is preceded by a newline.                   |
  9.  | For this program to work, the EOL code in the text must be |
  10.  | with the OS being run.  Use StripCR or AddCR as necessary. |
  11.  +------------------------------------------------------------*/
  12.                                                 
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #define OUTBUFSIZ 16384
  17.  
  18. unsigned short replace, bufptr = 0;
  19. signed char *buf = NULL;
  20. FILE *infile, *outfile;
  21. long inpos = 0L, outpos = 0L;       /* file position trackers */
  22.  
  23. int writebuf(void);
  24.  
  25.  
  26. void main(int argc, char *argv[])
  27. {
  28. short err = 0,
  29.    plast, ptr = 1, pnext;  /* pointers to characters in lookahead buffer */
  30. signed char lk[3];         /* lookahead buffer */
  31.  
  32. if (argc != 3 && argc != 2) {
  33.    fputs("StripHR by Unicorn Research Corporation\nIntelligently strip unwanted hard returns from text files\nwhile retaining paragraph formatting.\n\nUsage:  StripHR infile [outfile]\n\n", stdout);
  34.    fputs("If outfile is omitted, then infile will be replaced.\nA hard return is kept if it is followed by any white space (e.g.\ntabs, spaces, returns) or if preceded by another hard return.\n", stdout);
  35.    fputs("The end-of-line code in the text file must be compatible with your\noperating system -- use StripCR / AddCR as necessary.\n", stdout);
  36.    return;
  37.    }
  38. if ((infile = fopen( argv[1], (replace = (argc == 2)) ? "r+" : "r" )) == NULL) {
  39.    fputs("Could not open input file.\n", stdout);
  40.    return;
  41.    }
  42. if (replace)
  43.    outfile = infile;
  44. else if ((outfile = fopen( argv[2], "w" )) == NULL) {
  45.    fputs("Could not open output file.\n", stdout);
  46.    fclose(infile);
  47.    return;
  48.    }
  49. if ((buf = (signed char *)malloc(OUTBUFSIZ)) == NULL) {
  50.    fputs("Not enough memory!\n", stdout);
  51.    goto cleanup;
  52.    }
  53.  
  54. for (inpos = 0; inpos < 3; inpos++)  /* read first three characters */
  55.    if ((lk[inpos] = fgetc(infile)) == EOF)
  56.       { err = 2;  goto clean2; }
  57. buf[bufptr++] = lk[0];               /* record first char in output buffer */
  58.  
  59. do {  /* process the file */
  60.    plast = (ptr + 2) % 3;
  61.    pnext = (ptr + 1) % 3;
  62.    if (lk[ptr] != '\n'                                /* if not newline */
  63.        || (lk[ptr] == '\n' &&                         /* or if newline and */
  64.        (isspace(lk[pnext]) || lk[plast] == '\n'))) {  /* whitespace, then */
  65.       buf[bufptr++] = lk[ptr];                        /* keep the character */
  66.       if (replace) inpos = ftell(infile);
  67.       if (bufptr == OUTBUFSIZ) if (err = writebuf()) break;
  68.       }
  69.    else {  /* otherwise substitute a space for the newline */
  70.       buf[bufptr++] = ' ';
  71.       if (replace) inpos = ftell(infile);
  72.       if (bufptr == OUTBUFSIZ) if (err = writebuf()) break;
  73.       }
  74.    ptr = pnext;
  75.    } while ((lk[plast] = fgetc(infile)) != EOF);
  76.  
  77. buf[bufptr++] = lk[pnext];          /* write out last character */
  78. if (!err && bufptr) err = writebuf();
  79.  
  80. clean2:
  81. if (err == 1)
  82.    fputs("Error writing output file.\n", stdout);
  83. else if (err == 2)
  84.    fputs("Error reading input file.\n", stdout);
  85. else if (replace) {
  86.    fseek(outfile, outpos, 0);
  87.    fputc('\x1A', outfile);  /* output a ctrl-Z */
  88.    fputs(argv[1], stdout);
  89.    fputs(" is now stripped of offending hard returns.\nDelete all text past the ctrl-Z character.\n", stdout);
  90.    }
  91. else {
  92.    fputs("HR-Stripped file ", stdout);
  93.    fputs(argv[2], stdout);
  94.    fputs(" successfully created.\n", stdout);
  95.    }
  96.  
  97. cleanup:
  98. free(buf);
  99. fclose(infile);
  100. if (!replace) fclose(outfile);
  101. }
  102.  
  103.  
  104. /* write the buffer to the output file, when full or when done */
  105. int writebuf()
  106. {
  107. unsigned short i = 0;
  108. if (replace) if (fseek(outfile, outpos, 0)) return 1;
  109. while (i < bufptr)
  110.    if (fputc(buf[i++], outfile) == EOF) return 1;
  111. if (replace) {
  112.    outpos = ftell(outfile);
  113.    if (fseek(infile, inpos, 0)) return 2;
  114.    }
  115. bufptr = 0;
  116. return 0;
  117. }
  118.